package cn.edu.ecut.controller;


import com.mozicoding.foundation.support.GraphicHelper;
import com.mozicoding.web.context.ParameterHolder;
import com.mozicoding.web.context.ResponseHolder;
import com.mozicoding.web.support.Controller;
import com.mozicoding.web.support.RedirectAttributes;
import com.mozicoding.web.support.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@Controller( path = "/teacher" )
public class TeacherController {

    @RequestMapping( path = "/page/login" )
    public String loginPage(){
        System.out.println( "= = = = = loginPage = = = = =" );
        return "/WEB-INF/assets/login.html" ; // forward:/WEB-INF/assets/login.html
    }

    @RequestMapping( path = "/action/login" )
    public String loginAction(ParameterHolder holder , HttpSession session , RedirectAttributes attrs ){
        System.out.println( "= = = = = loginAction = = = = =" );

        // 通过 ParameterHolder 对象获取表单中数据
        String uname = holder.getString( "username" );
        String passwd = holder.getString( "password" );

        // 检查用户是否输入了用户名
        if( uname == null || uname.trim().isEmpty() ) {
            attrs.addFlashAttribute( "errorMessage" , "用户名不能为空" );
            return "redirect:/teacher/page/login" ; // 重定向到 登录页面
        }
        // 检查用户是否输入了密码
        if( passwd == null || passwd.trim().isEmpty() ) {
            attrs.addFlashAttribute( "errorMessage" , "密码不能为空" );
            return "redirect:/teacher/page/login" ; // 重定向到 登录页面
        }

        // 将用户信息存储到会话对象中并去往主页面
        session.setAttribute( "teacherName" , uname ); // 设置属性
        return "redirect:/teacher/page/home" ; // 重定向 主页面
    }

    @RequestMapping( path = "/page/home" )
    public String homePage(){
        System.out.println( "= = = = = homePage = = = = =" );
        return "/WEB-INF/assets/home.html" ; // forward:/WEB-INF/assets/login.html
    }

    @RequestMapping( path = "/action/logout" )
    public String logoutAction(HttpSession session ){
        System.out.println( "= = = = = logoutAction = = = = =" );
        session.removeAttribute( "teacherName" ); // 删除属性
        return "redirect:/teacher/page/home" ; // 重定向
    }

    @RequestMapping( path = "/captcha")
    public void captcha( HttpSession session){
        System.out.println( "= = = = = lcaptcha = = = = =" );

        // 获得当前的响应对象(HttpServletResponse)
        HttpServletResponse response = ResponseHolder.getCurrentResponse();

        try {
            // 获得可以输出验证码图片的字节流
            OutputStream output = response.getOutputStream();
            // 调用 GraphicHelper 类中的 create方法生成验证码图片并通过 output 输出该图片
            // 同时将 验证码图片上的内容 记录到一个 String 变量中
            String code = GraphicHelper.create( 6 , false , 180 , 40 , output , 50 );
            System.out.println( "生成的图片验证码内容是: " + code );

            // 将生成的验证码图片的内容保存到当前会话对象中
            session.setAttribute( "CAPTCHA" , code );// code 所对应的字符串就是需要保存的内容
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

}
